<template>
  <div id="app">
    <h2>测试题</h2>
    <SubjectItem
      @result="resultFn(arguments)"
      :data-id="index"
      v-for="(item, index) in list"
      :key="index"
    ></SubjectItem>
    <div>
      <FlagItem :list="listData"></FlagItem>
    </div>
  </div>
</template>

<script>
// 难点是子组件传递两个参数，用arguments接收
import SubjectItem from './components/SubjectItem.vue'
import FlagItem from './components/FlagItem.vue'
export default {
  components: {
    SubjectItem,
    FlagItem
  },
  data() {
    return {
      flag: '',
      list: ['', '', '', '', ''],
      listData: ['未完成', '未完成', '未完成', '未完成', '未完成']
    }
  },
  methods: {
    resultFn(data) {
      // 数组的监听是只有当数组结构改变时才能监听到，所以这里用splice方法
      this.list.splice([data[0]], 1)
      this.list.splice([data[0]], 0, data[1])
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        this.listData = newValue.map(element => {
          if (element === '') {
            return (element = '未完成')
          } else if (element) {
            return (element = '回答正确')
          } else {
            return (element = '回答错误')
          }
        })
      }
    }
  }
}
</script>

<style>
body {
  background-color: #eee;
}

#app {
  background-color: #fff;
  width: 500px;
  margin: 50px auto;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  padding: 2em;
}
</style>
